#include <GLFW/glfw3.h>
// for native apis
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#include <GLFW/glfw3native.h>
#include<Windows.h>
#include <stdio.h>
#include <stdlib.h>

static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}

int main(void)
{
    GLboolean running = GL_TRUE;
    GLFWwindow* window;

    glfwSetErrorCallback(error_callback);
	MessageBox(0, "init", "extention part test", MB_OK);
    if (!glfwInit())
        exit(EXIT_FAILURE);

    // glfwWindowHint(GLFW_VISIBLE, GL_FALSE);

	glfwMakeOpenGLDisabled();
	MessageBox(0, "create a window", "extention part test", MB_OK);
    window = glfwCreateWindow(200, 200, "test", NULL, NULL);
    if (!window)
    {
		MessageBox(0, "failed to create the window", "extention part test", MB_OK);
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
	MessageBox(0, "move the window", "extention part test", MB_OK);
    glfwSetWindowPos(window, 100, 100);
	// MessageBox(0, "show the window", "extention part test", MB_OK);
    // glfwShowWindow(window);
	MessageBox(0, "rename title of the window in order to check the handle is valid", "extention part test", MB_OK);
	SetWindowTextA(glfwGetWin32Window(window), "renamed");
    while (running)
    {
        if (glfwWindowShouldClose(window)){
			MessageBox(0, "the window is closing", "extention part test", MB_OK);
            running = GL_FALSE;
		}
        glfwPollEvents();
    }
	// glfwTerminate();
	MessageBox(0, "making opengl enabled and recreating the window", "extention part test", MB_OK);
	glfwMakeOpenGLEnabled();
	// if (!glfwInit())
    //     exit(EXIT_FAILURE);
	running = GL_TRUE;
	glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
	
	window = glfwCreateWindow(200, 200, "test", NULL, NULL);
	if (!window)
    {
		MessageBox(0, "failed to create the window", "extention part test", MB_OK);
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
	
	glfwMakeContextCurrent(window);
    glClearColor(1.f,
                 0.f,
                 0.f,
                 0.f);

    glfwShowWindow(window);

	while (running)
    {
		glfwMakeContextCurrent(window);
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        if (glfwWindowShouldClose(window)){
			MessageBox(0, "the window is closing", "extention part test", MB_OK);
            running = GL_FALSE;
		}
        glfwPollEvents();
    }

	MessageBox(0, "test completed", "extention part test", MB_OK);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

